﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using API_Service.BizLogic.WipLogApi;
using Nancy;
using Newtonsoft.Json;

namespace API_Service.Module
{
    public class RouteDesignModule : BaseAPIModule
    {

        private RouteDesigerService designService = new RouteDesigerService();

        public RouteDesignModule() : base("/routedesign")
        {
            Get["/"] = _ => Response.AsJson("Route Desiger module");

            # region route design control module

            Get["/list"] = _ =>
            {
                ExecutionResult<dynamic> result = designService.GetRouteList();

                return Response.AsJson(result);
            };

            Get["/designlist"] = _ =>
            {
                ExecutionResult<dynamic> result = designService.GetRouteDesignList();

                return Response.AsJson(result);
            };

            Get["/designlist/{routecode}"] = p =>
            {
                ExecutionResult<dynamic> result = designService.GetRouteDesignListByID((string)p.routecode);

                return Response.AsJson(result);
            };

            Get["/list/byid/{routecode}"] = p =>
            {
                ExecutionResult<dynamic> result = designService.GetRouteByID((string)p.routecode);

                return Response.AsJson(result);
            };

            Get["/list/byname/{routename}"] = p =>
            {
                ExecutionResult<dynamic> result = designService.GetRouteByName((string)p.routename);

                return Response.AsJson(result);
            };

            Get["/list/loadroutedesign/{routecode}"] = p =>
            {
                ExecutionResult<dynamic> result = designService.LoadRouteDesign((string)p.routecode);

                List<dynamic> list = new List<dynamic>();

                foreach (var v in result.Datas)
                {
                    list.Add(new
                    {
                        route_code = v.route_code,
                        route_info = Encoding.Default.GetString(v.route_info),
                        create_time = v.create_time,
                        create_user = v.create_user,
                        update_time = v.update_time,
                        update_user = v.update_time
                    });
                }

                result.Datas = list.ToArray();

                return Response.AsJson(result);
            };

            Post["/saveroutecontrol"] = _ =>
            {
                try
                {
                    string viewModel = (string)Request.Form["MODEL"];

                    ExecutionResult<dynamic> result = designService.SaveRouteControlInfo(viewModel);

                    return Response.AsJson(result);
                }
                catch (Exception ex)
                {
                    ExecutionResult<dynamic> result = new ExecutionResult<dynamic>
                    {
                        Message = ex.Message,
                        Status = false
                    };
                    return Response.AsJson(result);
                }

            };

            Post["/saveroutename"] = _ =>
            {
                try
                {
                    string viewModel = (string)Request.Form["MODEL"];

                    ExecutionResult<dynamic> result = designService.SaveRouteNameInfo(viewModel);

                    return Response.AsJson(result);
                }
                catch (Exception ex)
                {
                    ExecutionResult<dynamic> result = new ExecutionResult<dynamic>
                    {
                        Message = ex.Message,
                        Status = false
                    };
                    return Response.AsJson(result);
                }

            };

            Post["/saveroutedesign"] = _ =>
            {
                try
                {
                    string routecode = (string)Request.Form["routecode"];

                    byte[] routeinfo = Encoding.Default.GetBytes((string)Request.Form["routeinfo"]);

                    string updateuser = (string)Request.Form["updateuser"];

                    ExecutionResult<dynamic> result = designService.SaveRouteDesignInfo(routecode, routeinfo, updateuser);

                    return Response.AsJson(result);
                }
                catch (Exception ex)
                {
                    ExecutionResult<dynamic> result = new ExecutionResult<dynamic>
                    {
                        Message = ex.Message,
                        Status = false
                    };
                    return Response.AsJson(result);
                }
               
            };

            Post["/deleterouteInfo"] = _ =>
            {
                try
                {
                    string routecode = (string)Request.Form["routecode"];

                    ExecutionResult<dynamic> result = designService.DeleteRouteInfo(routecode);

                    return Response.AsJson(result);
                }
                catch (Exception ex)
                {
                    ExecutionResult<dynamic> result = new ExecutionResult<dynamic>
                    {
                        Message = ex.Message,
                        Status = false
                    };
                    return Response.AsJson(result);
                }

            };

            Post["/routenameinsert"] = _ =>
            {
                try
                {
                    string vm = (string)Request.Form["MODEL"];

                    ExecutionResult<dynamic> result = designService.InsertRouteNameInfo(vm);

                    return Response.AsJson(result);
                }
                catch (Exception ex)
                {
                    ExecutionResult<dynamic> result = new ExecutionResult<dynamic>
                    {
                        Message = ex.Message,
                        Status = false
                    };
                    return Response.AsJson(result);
                }

            };

            Post["/updateroutename"] = _ =>
            {
                try
                {
                    string vm = (string)Request.Form["MODEL"];

                    ExecutionResult<dynamic> result = designService.UpdateRouteName(vm);

                    return Response.AsJson(result);
                }
                catch (Exception ex)
                {
                    ExecutionResult<dynamic> result = new ExecutionResult<dynamic>
                    {
                        Message = ex.Message,
                        Status = false
                    };
                    return Response.AsJson(result);
                }

            };

            #endregion

        }



    }
}